#!/usr/bin/env python

def _(s): return s

from frontend.dialogs import UserMessage
import Curry
import os, sys, wx

class ImportHandler:
    _shared_state = {}
    
    def __init__(self):
        self.__dict__ = self._shared_state
        self.curry = Curry.curry
    
    def setState(self, mainWindow):
        self.mainWindow = mainWindow
        return self
    
    def doImport(self, className, event):
        try:
            mod = "frontend.eventHandlers.%s" % className
            __import__(mod)
            theModule = sys.modules[mod]
            getattr(theModule, className)(self.mainWindow)
        except(Exception), error:
            UserMessage.say(_("Unable to import module: %s" % error),
                           _("Import Error!"),
                           wx.ICON_ERROR)
    
    def bindMenuEvent(self, item, module):
        curried = self.curry(self.doImport, module)
        self.mainWindow.Bind(wx.EVT_MENU, curried, item)
        
    def bindExtraEvents(self):
        self.mainWindow.Bind(wx.EVT_CLOSE, self.curry(self.doImport,
                                                       "CloseWindow"))
        self.mainWindow.Bind(wx.EVT_MENU_RANGE,
                             self.curry(self.doImport, "FileHistory"),
                             id=wx.ID_FILE1, id2=wx.ID_FILE9)
    